<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Maven依赖管理指南 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .serif {
            font-family: 'Noto Serif SC', serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #2d2d2d;
            border-left: 4px solid #a777e3;
        }
        .hover-scale {
            transition: transform 0.2s ease;
        }
        .hover-scale:hover {
            transform: translateY(-2px);
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
            background-color: #f8f9fa;
            padding: 1.5rem;
            border-radius: 0.5rem;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4">
        <div class="container mx-auto max-w-6xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-4 serif">Maven 依赖管理权威指南</h1>
                <p class="text-xl md:text-2xl max-w-3xl mb-8 opacity-90">掌握依赖管理艺术，解决 Java 项目中的版本冲突问题</p>
                <div class="flex space-x-4">
                    <a href="#scope" class="px-6 py-3 bg-white text-purple-700 rounded-full font-medium hover:bg-gray-100 transition duration-300">
                        <i class="fas fa-book-open mr-2"></i>Scope 详解
                    </a>
                    <a href="#conflict" class="px-6 py-3 bg-transparent border-2 border-white text-white rounded-full font-medium hover:bg-white hover:text-purple-700 transition duration-300">
                        <i class="fas fa-fire-extinguisher mr-2"></i>冲突解决
                    </a>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-6xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8 hover-scale">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 serif">为什么需要依赖管理？</h2>
                <p class="text-lg text-gray-700 mb-6">在现代 Java 开发中，项目往往依赖数十甚至上百个第三方库。Maven 作为最流行的构建工具之一，其依赖管理机制帮助我们自动化处理这些复杂的依赖关系。</p>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                    <div class="bg-blue-50 p-6 rounded-lg">
                        <div class="flex items-center mb-3">
                            <div class="bg-blue-100 p-3 rounded-full mr-4">
                                <i class="fas fa-check-circle text-blue-600 text-xl"></i>
                            </div>
                            <h3 class="text-xl font-semibold text-gray-800">自动化依赖解析</h3>
                        </div>
                        <p class="text-gray-700">Maven 自动下载并管理项目所需的所有依赖库及其传递依赖，简化了开发者的工作。</p>
                    </div>
                    <div class="bg-purple-50 p-6 rounded-lg">
                        <div class="flex items-center mb-3">
                            <div class="bg-purple-100 p-3 rounded-full mr-4">
                                <i class="fas fa-exclamation-triangle text-purple-600 text-xl"></i>
                            </div>
                            <h3 class="text-xl font-semibold text-gray-800">常见挑战</h3>
                        </div>
                        <p class="text-gray-700">当不同模块依赖同一库的不同版本时，会导致依赖冲突，引发类加载异常或方法缺失问题。</p>
                    </div>
                </div>

                <div class="mermaid">
                    <div class="mermaid">
                        graph TD
                            A[项目] --> B[模块A]
                            A --> C[模块B]
                            B --> D[common-lib v1.0]
                            C --> E[common-lib v2.0]
                            D --> F[冲突发生]
                            E --> F
                    </div>
                </div>
            </div>
        </section>

        <!-- Scope Section -->
        <section id="scope" class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8 hover-scale">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 serif">
                    <span class="text-purple-600 mr-2"><i class="fas fa-layer-group"></i></span>
                    Maven 依赖 Scope 详解
                </h2>
                <p class="text-lg text-gray-700 mb-8">Scope 定义了依赖在项目生命周期中的可用范围，合理使用 Scope 可以优化构建过程和最终产物。</p>

                <div class="overflow-x-auto mb-10">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Scope</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">作用</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">适用场景</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">compile</span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">默认范围，依赖在编译、测试、运行和打包时均可用</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">适用于核心库，如 Spring Boot</td>
                            </tr>
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-blue-100 text-blue-800">provided</span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">依赖在编译和测试时可用，但不会打包</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">适用于 Servlet API、JDK 自带库</td>
                            </tr>
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-yellow-100 text-yellow-800">runtime</span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">依赖不会用于编译，但在运行和测试时可用</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">适用于 JDBC 驱动等运行时依赖</td>
                            </tr>
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800">test</span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">仅用于测试编译和运行，打包时不可用</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">适用于 JUnit、Mockito 等测试库</td>
                            </tr>
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">system</span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">类似 provided，但需要手动指定 jar 路径</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">不推荐使用</td>
                            </tr>
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-indigo-100 text-indigo-800">import</span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">仅用于 dependencyManagement，用于引入 BOM</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">适用于 Spring Boot Starter 依赖管理</td>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <!-- compile Example -->
                    <div class="bg-white border border-gray-200 rounded-lg overflow-hidden">
                        <div class="bg-green-50 px-4 py-3 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-800 flex items-center">
                                <span class="bg-green-100 text-green-800 px-2 py-1 rounded-md text-xs mr-2">compile</span>
                                core-lib 示例
                            </h3>
                        </div>
                        <div class="p-4 code-block text-gray-200 overflow-x-auto">
                            <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.ts&lt;/groupId&gt;
    &lt;artifactId&gt;core-lib&lt;/artifactId&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
    &lt;scope&gt;compile&lt;/scope&gt;
&lt;/dependency&gt;</code></pre>
                        </div>
                        <div class="px-4 py-3 bg-gray-50 text-sm text-gray-600">
                            <i class="fas fa-info-circle mr-2"></i> core-lib 依赖在整个构建周期都可用，并且会被打包进最终的 JAR 或 WAR。
                        </div>
                    </div>

                    <!-- provided Example -->
                    <div class="bg-white border border-gray-200 rounded-lg overflow-hidden">
                        <div class="bg-blue-50 px-4 py-3 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-800 flex items-center">
                                <span class="bg-blue-100 text-blue-800 px-2 py-1 rounded-md text-xs mr-2">provided</span>
                                javax.servlet-api 示例
                            </h3>
                        </div>
                        <div class="p-4 code-block text-gray-200 overflow-x-auto">
                            <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
    &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
    &lt;version&gt;4.0.1&lt;/version&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;</code></pre>
                        </div>
                        <div class="px-4 py-3 bg-gray-50 text-sm text-gray-600">
                            <i class="fas fa-info-circle mr-2"></i> javax.servlet-api 仅在编译和测试时可用，不会被打包，因为 Tomcat/Jetty 运行时已经提供了该依赖。
                        </div>
                    </div>

                    <!-- runtime Example -->
                    <div class="bg-white border border-gray-200 rounded-lg overflow-hidden">
                        <div class="bg-yellow-50 px-4 py-3 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-800 flex items-center">
                                <span class="bg-yellow-100 text-yellow-800 px-2 py-1 rounded-md text-xs mr-2">runtime</span>
                                mysql-connector-java 示例
                            </h3>
                        </div>
                        <div class="p-4 code-block text-gray-200 overflow-x-auto">
                            <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;mysql&lt;/groupId&gt;
    &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
    &lt;version&gt;8.0.28&lt;/version&gt;
    &lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;</code></pre>
                        </div>
                        <div class="px-4 py-3 bg-gray-50 text-sm text-gray-600">
                            <i class="fas fa-info-circle mr-2"></i> MySQL 驱动程序不需要在编译时存在，但运行时必须可用。
                        </div>
                    </div>

                    <!-- test Example -->
                    <div class="bg-white border border-gray-200 rounded-lg overflow-hidden">
                        <div class="bg-red-50 px-4 py-3 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-800 flex items-center">
                                <span class="bg-red-100 text-red-800 px-2 py-1 rounded-md text-xs mr-2">test</span>
                                junit-jupiter-api 示例
                            </h3>
                        </div>
                        <div class="p-4 code-block text-gray-200 overflow-x-auto">
                            <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
    &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
    &lt;version&gt;5.7.2&lt;/version&gt;
    &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;</code></pre>
                        </div>
                        <div class="px-4 py-3 bg-gray-50 text-sm text-gray-600">
                            <i class="fas fa-info-circle mr-2"></i> JUnit 依赖仅用于测试阶段，打包时不会包含。
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Conflict Section -->
        <section id="conflict" class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8 hover-scale">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 serif">
                    <span class="text-red-500 mr-2"><i class="fas fa-fire"></i></span>
                    依赖冲突的来源与解决方案
                </h2>
                <p class="text-lg text-gray-700 mb-8">在大型项目中，往往会有多个模块和第三方库，可能会间接引入相同的依赖但版本不同，导致冲突。</p>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-8 mb-12">
                    <!-- Conflict Sources -->
                    <div>
                        <h3 class="text-2xl font-semibold mb-4 text-gray-800 border-b pb-2 serif">冲突来源</h3>
                        
                        <div class="mb-6">
                            <h4 class="text-lg font-medium mb-2 text-gray-800 flex items-center">
                                <i class="fas fa-code-branch text-blue-500 mr-2"></i>
                                直接依赖版本不同
                            </h4>
                            <div class="p-4 code-block text-gray-200 overflow-x-auto">
                                <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.ts&lt;/groupId&gt;
    &lt;artifactId&gt;common-lib&lt;/artifactId&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
    &lt;groupId&gt;org.ts&lt;/groupId&gt;
    &lt;artifactId&gt;common-lib&lt;/artifactId&gt;
    &lt;version&gt;2.0.0&lt;/version&gt;
&lt;/dependency&gt;</code></pre>
                            </div>
                        </div>

                        <div>
                            <h4 class="text-lg font-medium mb-2 text-gray-800 flex items-center">
                                <i class="fas fa-project-diagram text-purple-500 mr-2"></i>
                                传递依赖版本不同
                            </h4>
                            <div class="p-4 code-block text-gray-200 overflow-x-auto">
                                <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.ts&lt;/groupId&gt;
    &lt;artifactId&gt;service-a&lt;/artifactId&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
    &lt;groupId&gt;org.ts&lt;/groupId&gt;
    &lt;artifactId&gt;service-b&lt;/artifactId&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
&lt;/dependency&gt;</code></pre>
                            </div>
                            <p class="mt-2 text-sm text-gray-600">service-a 依赖 common-lib:1.0.0，但 service-b 依赖 common-lib:2.0.0，会导致冲突。</p>
                        </div>
                    </div>

                    <!-- Solution Visual -->
                    <div>
                        <div class="mermaid">
                            graph LR
                                A[依赖冲突] --> B[使用 dependency:tree 分析]
                                A --> C[锁定版本]
                                A --> D[排除冲突依赖]
                                B --> E[确定冲突位置]
                                C --> F[在 dependencyManagement 中指定]
                                D --> G[排除特定传递依赖]
                                E --> H[解决问题]
                                F --> H
                                G --> H
                        </div>
                    </div>
                </div>

                <h3 class="text-2xl font-semibold mb-4 text-gray-800 border-b pb-2 serif">解决方案</h3>
                
                <div class="space-y-8">
                    <!-- Solution 1 -->
                    <div class="bg-gray-50 p-6 rounded-lg">
                        <h4 class="text-xl font-semibold mb-3 text-gray-800 flex items-center">
                            <span class="bg-green-500 text-white px-3 py-1 rounded-md text-sm mr-3">方案 1</span>
                            使用 dependency:tree 检查依赖
                        </h4>
                        <p class="text-gray-700 mb-4">Maven 提供 dependency:tree 命令来查看依赖关系：</p>
                        <div class="p-4 code-block text-gray-200 overflow-x-auto">
                            <pre><code>mvn dependency:tree</code></pre>
                        </div>
                        <p class="mt-2 text-gray-700">执行后会输出完整的依赖树，帮助分析哪个依赖引入了冲突的版本。</p>
                    </div>

                    <!-- Solution 2 -->
                    <div class="bg-gray-50 p-6 rounded-lg">
                        <h4 class="text-xl font-semibold mb-3 text-gray-800 flex items-center">
                            <span class="bg-blue-500 text-white px-3 py-1 rounded-md text-sm mr-3">方案 2</span>
                            在 dependencyManagement 中锁定版本
                        </h4>
                        <p class="text-gray-700 mb-4">dependencyManagement 允许父 POM 统一管理版本，避免子模块使用不同版本：</p>
                        <div class="p-4 code-block text-gray-200 overflow-x-auto">
                            <pre><code>&lt;dependencyManagement&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.ts&lt;/groupId&gt;
            &lt;artifactId&gt;common-lib&lt;/artifactId&gt;
            &lt;version&gt;1.5.0&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/dependencyManagement&gt;</code></pre>
                        </div>
                        <p class="mt-2 text-gray-700">这样，所有子模块引用 common-lib 时都会使用 1.5.0 版本。</p>
                    </div>

                    <!-- Solution 3 -->
                    <div class="bg-gray-50 p-6 rounded-lg">
                        <h4 class="text-xl font-semibold mb-3 text-gray-800 flex items-center">
                            <span class="bg-purple-500 text-white px-3 py-1 rounded-md text-sm mr-3">方案 3</span>
                            排除冲突依赖
                        </h4>
                        <p class="text-gray-700 mb-4">可以在 pom.xml 里显式排除某些依赖，防止版本冲突：</p>
                        <div class="p-4 code-block text-gray-200 overflow-x-auto">
                            <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.ts&lt;/groupId&gt;
    &lt;artifactId&gt;service-a&lt;/artifactId&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
    &lt;exclusions&gt;
        &lt;exclusion&gt;
            &lt;groupId&gt;org.ts&lt;/groupId&gt;
            &lt;artifactId&gt;common-lib&lt;/artifactId&gt;
        &lt;/exclusion&gt;
    &lt;/exclusions&gt;
&lt;/dependency&gt;</code></pre>
                        </div>
                        <p class="mt-2 text-gray-700">此配置排除了 service-a 依赖的 common-lib，确保 service-b 的版本不会被覆盖。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary -->
        <section class="mb-16">
            <div class="bg-gradient-to-r from-purple-500 to-blue-500 rounded-xl shadow-lg p-8 text-white hover-scale">
                <h2 class="text-3xl font-bold mb-4 serif">关键要点总结</h2>
                <ul class="space-y-3 text-lg">
                    <li class="flex items-start">
                        <span class="bg-white text-purple-600 rounded-full w-6 h-6 flex items-center justify-center mr-3 mt-1 flex-shrink-0">
                            <i class="fas fa-check text-xs"></i>
                        </span>
                        <span>合理使用 Scope 可以优化构建过程并减少最终包大小</span>
                    </li>
                    <li class="flex items-start">
                        <span class="bg-white text-purple-600 rounded-full w-6 h-6 flex items-center justify-center mr-3 mt-1 flex-shrink-0">
                            <i class="fas fa-check text-xs"></i>
                        </span>
                        <span>依赖冲突通常由直接或间接依赖的不同版本引起</span>
                    </li>
                    <li class="flex items-start">
                        <span class="bg-white text-purple-600 rounded-full w-6 h-6 flex items-center justify-center mr-3 mt-1 flex-shrink-0">
                            <i class="fas fa-check text-xs"></i>
                        </span>
                        <span>使用 dependency:tree 分析依赖关系是解决冲突的第一步</span>
                    </li>
                    <li class="flex items-start">
                        <span class="bg-white text-purple-600 rounded-full w-6 h-6 flex items-center justify-center mr-3 mt-1 flex-shrink-0">
                            <i class="fas fa-check text-xs"></i>
                        </span>
                        <span>dependencyManagement 和 exclusions 是解决冲突的有效手段</span>
                    </li>
                </ul>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="container mx-auto max-w-6xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-semibold">技术小馆</h3>
                    <p class="text-gray-400 mt-1">专业的开发者知识分享平台</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition duration-300 flex items-center">
                        <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-6 pt-6 text-center text-gray-500 text-sm">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>